home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / httpd / cgi-src / change-passwd.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  5KB  |  172 lines

  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/signal.h>
  5. #include <stdlib.h>
  6.  
  7. #define USER_FILE "/usr/local/etc/httpd/conf/.htpasswd"
  8. #define WIZARD "surobm"
  9.  
  10. char *makeword(char *line, char stop);
  11. char *fmakeword(FILE *f, char stop, int *len);
  12. char x2c(char *what);
  13. void unescape_url(char *url);
  14. void plustospace(char *str);
  15.  
  16. char *crypt(char *pw, char *salt); /* why aren't these prototyped in include */
  17.  
  18.  
  19. char *tn;
  20.  
  21. /* From local_passwd.c (C) Regents of Univ. of California blah blah */
  22. static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
  23.         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  24.  
  25. to64(s, v, n)
  26.   register char *s;
  27.   register long v;
  28.   register int n;
  29. {
  30.     while (--n >= 0) {
  31.         *s++ = itoa64[v&0x3f];
  32.         v >>= 6;
  33.     }
  34. }
  35.  
  36. void change_password(char *user, char *pw, FILE *f) {
  37.     char *cpw, salt[3];
  38.  
  39.     (void)srand((int)time((time_t *)NULL));
  40.     to64(&salt[0],rand(),2);
  41.     cpw = crypt(pw,salt);
  42.     free(pw);
  43.     fprintf(f,"%s:%s\n",user,cpw);
  44. }
  45.  
  46. void putline(FILE *f,char *l) {
  47.     int x;
  48.  
  49.     for(x=0;l[x];x++) fputc(l[x],f);
  50.     fputc('\n',f);
  51. }
  52.  
  53. main(int argc, char *argv[]) {
  54.     register int x;
  55.     int cl,found,create;
  56.     char *u,*t1,*t2,*p1,*p2,*user, command[256], line[256], l[256], w[256];
  57.     FILE *tfp,*f;
  58.  
  59.     tn = NULL;
  60.  
  61.     printf("Content-type: text/html%c%c",10,10);
  62.  
  63.     if(strcmp(getenv("REQUEST_METHOD"),"POST")) {
  64.         printf("This script should be referenced with a METHOD of POST.\n");
  65.         printf("If you don't understand this, see this ");
  66.         printf("<A HREF=\"http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overview.html\">forms overview</A>.%c",10);
  67.         exit(1);
  68.     }
  69.     if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded")) {
  70.         printf("This script can only be used to decode form results. \n");
  71.         exit(1);
  72.     }
  73.     cl = atoi(getenv("CONTENT_LENGTH"));
  74.  
  75.     user=NULL;
  76.     p1=NULL;
  77.     p2=NULL;
  78.     create=0;
  79.     for(x=0;cl && (!feof(stdin));x++) {
  80.         t1 = fmakeword(stdin,'&',&cl);
  81.         t2 = makeword(t1,'=');
  82.         unescape_url(t1);
  83.         unescape_url(t2);
  84.         if(!strcmp(t2,"user")) {
  85.             if(!user)
  86.                 user = t1;
  87.             else {
  88.                 printf("This script was accessed from the wrong form.\n");
  89.                 exit(1);
  90.             }
  91.         }
  92.         else if(!strcmp(t2,"newpasswd1")) {
  93.             if(!p1)
  94.                 p1 = t1;
  95.             else {
  96.                 printf("This script was accessed from the wrong form.\n");
  97.                 exit(1);
  98.             }
  99.         }
  100.         else if(!strcmp(t2,"newpasswd2")) {
  101.             if(!p2)
  102.                 p2 = t1;
  103.             else {
  104.                 printf("This script was accessed from the wrong form.\n");
  105.                 exit(1);
  106.             }
  107.         }
  108.         else {
  109.             printf("This script was accessed from the wrong form.\n");
  110.             printf("Unrecognized directive %s.\n",t2);
  111.             exit(1);
  112.         }
  113.         free(t2);
  114.     }
  115.     u=getenv("REMOTE_USER");
  116.     if((strcmp(u,WIZARD)) && (strcmp(user,u))) {
  117.             printf("<TITLE>User Mismatch</TITLE>");
  118.             printf("<H1>User Mismatch</H1>");
  119.             printf("The username you gave does not correspond with the ");
  120.             printf("user you authenticated as.\n");
  121.             exit(1);
  122.         }
  123.     if(strcmp(p1,p2)) {
  124.         printf("<TITLE>Password Mismatch</TITLE>");
  125.         printf("<H1>Password Mismatch</H1>");
  126.         printf("The two copies of your the password do not match. Please");
  127.         printf(" try again.");
  128.         exit(1);
  129.     }
  130.  
  131.     tn = tmpnam(NULL);
  132.     if(!(tfp = fopen(tn,"w"))) {
  133.         fprintf(stderr,"Could not open temp file.\n");
  134.         exit(1);
  135.     }
  136.  
  137.     if(!(f = fopen(USER_FILE,"r"))) {
  138.         fprintf(stderr,
  139.                 "Could not open passwd file for reading.\n",USER_FILE);
  140.         exit(1);
  141.     }
  142.  
  143.     found = 0;
  144.     while(!(getline(line,256,f))) {
  145.         if(found || (line[0] == '#') || (!line[0])) {
  146.             putline(tfp,line);
  147.             continue;
  148.         }
  149.         strcpy(l,line);
  150.         getword(w,l,':');
  151.         if(strcmp(user,w)) {
  152.             putline(tfp,line);
  153.             continue;
  154.         }
  155.         else {
  156.             change_password(user,p1,tfp);
  157.             found=1;
  158.         }
  159.     }
  160.     if((!found) && (create))
  161.         change_password(user,p1,tfp);
  162.     fclose(f);
  163.     fclose(tfp);
  164.     sprintf(command,"cp %s %s",tn,USER_FILE);
  165.     system(command);
  166.     unlink(tn);
  167.     printf("<TITLE>Successful Change</TITLE>");
  168.     printf("<H1>Successful Change</H1>");
  169.     printf("Your password has been successfully changed.<P>");
  170.     exit(0);
  171. }
  172.